<template>
    <div class="my-tag">
        <input class="input" type="text" placeholder="输入标签" v-if="isEdit" v-focus @keyup.enter="submit"
            @blur="blurEdit" />
        <div class="text" v-else @dblclick="changeEdit">{{ value }}</div>
    </div>
</template>
<script>
export default {
    name: 'App',
    props: {
        value: {
            type: String
        }
    },
    data() {
        return {
            isEdit: false
        }
    },
    methods: {
        changeEdit() {
            this.isEdit = true
        },
        blurEdit(e) {
            this.submitValue(e)
        },
        submit(e) {
            this.submitValue(e)
        },
        submitValue(e) {
            if (e.target.value.trim() === '') {
                alert('输入的内容不能为空')
                return
            }
            this.$emit('input', e.target.value)
            this.isEdit = false
        }
    },
}
</script>
<style scoped lang="less">
.my-tag {
    cursor: pointer;

    .input {
        appearance: none;
        outline: none;
        border: 1px solid #ccc;
        width: 100px;
        height: 40px;
        box-sizing: border-box;
        padding: 10px;
        color: #666;

        &::placeholder {
            color: #666;
        }
    }
}
</style>